package com.sqk.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sqk.reggie.common.Result;
import com.sqk.reggie.pojo.User;
import com.sqk.reggie.service.UserService;
import com.sqk.reggie.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;


/**
 * 用户管理
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;


    /**
     * 获取短信验证码(在本项目练习中，该路径不进行访问)
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/sendMsg")
    public Result<String> sendMsg(@RequestBody User user, HttpSession session){
        //获取手机号
        String phone = user.getPhone();
        log.info("phone = {}",phone);

        if (StringUtils.isNotEmpty(phone)){
            //随机生成验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code = {}",code);

            //通过阿里云短信服务发送短信
            //SMSUtils.sendMessage("短信签名","模板code","电话号码phone","验证码code");

            //存储验证码，保存到session中
            //session.setAttribute(phone,code);

            //优化，将验证码保存到redis中
            redisTemplate.opsForValue().set(phone,code,5L,TimeUnit.MINUTES);
            return Result.success("短信发送成功!");
        }

        return Result.error("短信发送失败!");
    }


    /**
     * 移动端用户登录
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public Result<Object> login(@RequestBody Map map, HttpSession session){
        log.info("map = {}" , map.get("phone").toString());

        String phone = (String) map.get("phone");
        String message = (String) map.get("message");

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq( User::getPhone, map.get("phone"));
        User one = userService.getOne(queryWrapper);

        if (one == null){
            User user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);
        }
        session.setAttribute("user",phone);

        return Result.success(user);

        /*
        //从redis中获取验证码
        String code = (String) redisTemplate.opsForValue().get(phone);

        //对比验证码
        if (code != null && message.equals(code)){
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq( User::getPhone, map.get("phone"));
            User one = userService.getOne(queryWrapper);

            if (one == null){
                User user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user",phone);
            redisTemplate.delete(phone);

            return Result.success(user);
        }


        return Result.error("登陆失败!");*/

    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @PostMapping("/loginout")
    public Result<String> logout(HttpSession session){
        session.removeAttribute("user");
        return Result.success("推出成功!");
    }

}
